package br.ufrj.arch;

/**
 * Classe de modelo que implementa um Bit.
 * 
 * @author Raphael
 * 
 */
public class Bit {
	private boolean value;

	public Bit() {
		super();
	}

	public Bit(boolean value) {
		super();
		this.value = value;
	}

	public Bit(char value) {
		super();
		this.value = parse(value);
	}

	private static boolean parse(char c) {
		return c == '0' ? false : true;
	}

	public static Bit parseBit(char value) {
		return new Bit(parse(value));
	}

	/**
	 * 
	 * @param bitString
	 *            String de zeros e uns.
	 * @return Array de Bits representado pela String.
	 */
	public static Bit[] parseBit(String bitString) {
		int length = bitString.length();
		String reverse = new StringBuffer(bitString).reverse().toString();
		Bit[] bits = new Bit[length];

		for (int i = 0; i < length; i++) {
			bits[i] = new Bit(reverse.charAt(i));
		}

		return bits;
	}

	public void setValue(boolean newValue) {
		value = newValue;
	}

	public void setValue(char newValue) {
		value = parse(newValue);
	}

	public boolean isSet() {
		return value;
	}

	public String toString() {
		return isSet() ? "1" : "0";
	}
}
